package com.springmvc.crud.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by BF100233 on 2016/10/9.
 */

/**
 * 实现接口的三个方法
 */
public class FileInterceptor implements HandlerInterceptor{

    /**
     * 在目标方法执行之前被调用
     *   可以用于设置权限、日志、事物
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     *          若返回值为：true: 继续调用后续的拦截器和目标方法
     *          若返回值为：false:后续的拦截器不会被调用，后续的目标方法也不会被调用。
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("[FileInterceptor] preHandle");
        return true;
    }

    /**
     * 调用目标方法之后，渲染视图之前被调用
     *   可以对请求域中的属性或视图做出修改。
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param modelAndView
     * @throws Exception
     */
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("[FileInterceptor] postHandle2");

    }

    /**
     * 在渲染视图之后被调用。
     *   释放资源。
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e
     * @throws Exception
     */
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("[FileInterceptor] afterCompletion");

    }
}
